package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.UserVoice;
import com.itheima.mapper.UserVoiceMapper;
import com.itheima.service.db.UserVoiceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class UserVoiceServiceImpl implements UserVoiceService {
    @Autowired
    private UserVoiceMapper userVoiceMapper;

    @Override
    public void updateTimes() {//每天12点自动调用这个方法

        UserVoice userVoice = new UserVoice(null,10);
        userVoiceMapper.update(userVoice,null);
    }

    //查询用户剩余次数
    @Override
    public Integer findTimesById(Long userId) {
        QueryWrapper<UserVoice> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);

        UserVoice userVoice = userVoiceMapper.selectOne(qw);
        return userVoice.getRemainingTimes();
    }

    //修改用户剩余次数
    @Override
    public void updateTimesById(Long userId, Integer times) {
        UserVoice userVoice = new UserVoice();
        userVoice.setUserId(userId);
        userVoice.setRemainingTimes(times);
        userVoiceMapper.updateById(userVoice);
    }


    @Override
    public void saveUserVoice(UserVoice userVoice) {
        userVoiceMapper.insert(userVoice);
    }
}
